<template>
  <div class="fillcontain" v-loading="dataListLoading">
    <bt-col-header title="规则组"></bt-col-header>
    <div class="page-content">
      <el-form :model="dataForm" class="" label-width="120px">
        <div class="row">
          <div class="col-md-6">
            <el-form-item label="规则组标识">
              <el-input v-model="dataForm.groupIdentify" placeholder="规则组标识" clearable></el-input>
            </el-form-item>
          </div>
          <div class="col-md-6">
            <el-form-item label="规则组名称">
              <el-input v-model="dataForm.groupName" placeholder="规则组名称" clearable></el-input>
              <!--            <el-select-->
              <!--              v-model="dataForm.groupName"-->
              <!--              filterable-->
              <!--              clearable-->
              <!--              placeholder="请选择"-->
              <!--            >-->
              <!--              <el-option-->
              <!--                v-for="(item, index) in allGroupInfo"-->
              <!--                :key="item.groupName + index"-->
              <!--                :label="item.groupName"-->
              <!--                :value="item.groupName"-->
              <!--              >-->
              <!--              </el-option>-->
              <!--            </el-select>-->
            </el-form-item>
          </div>
        </div>
        <div class="row">
          <div class="col-md-12 text-center mt-2">
            <rich-button :plain="true" type="info" shape="capsule" icon="search" @click="getDataList()">查询</rich-button>
            <rich-button shape="capsule" icon="quit" @click="resetData()">重置</rich-button>
          </div>
        </div>
      </el-form>  
    
    

      <div class="">
        <!-- 操作按钮 -->
        <div class="btn-padding left">
          <el-button :disabled="btnBoolean" type="info" icon="circle-plus" plain @click="addOrUpdateHandle()">新增
          </el-button>
          <el-button type="info" icon="circle-b-arrow" plain @click="downExcel()">下载所有规则组
          </el-button>
        </div>

        <!-- 查询结果列表 -->
        <div class="table_container">
          <!-- 列表 -->
          <el-table :data="dataList" border style="width: 100%">
            <el-table-column prop="groupIdentify" header-align="center" align="center" label="规则组标识">
            </el-table-column>
            <el-table-column prop="groupName" header-align="center" align="center" label="规则组名称">
            </el-table-column>
            <el-table-column prop="groupDesc" header-align="center" align="center" label="规则组描述">
            </el-table-column>
            <el-table-column prop="taskNo" header-align="center" align="center" label="任务号">
              <template slot-scope="scope">
                <span v-if="scope.row.type==='p'">{{scope.row.taskNo}}</span>
              </template>
            </el-table-column>
            <el-table-column prop="isEffect" header-align="center" align="center" label="状态">
              <template slot-scope="scope">
                {{ scope.row.isEffect | isEffectFilter }}
              </template>
            </el-table-column>
            <el-table-column header-align="center" align="center" label="操作">
              <template slot-scope="scope">
                <el-button :disabled="scope.row.disabled" v-if="isAuth('scene:edit')" type="text" size="small" @click="addOrUpdateHandle(scope.row)">
                  修改
                </el-button>
                <!-- <el-button
                v-if="scope.row.isEffect === '1'"
                :disabled="scope.row.disabled"
                type="text"
                size="small"
                @click="edit(scope.row)"
              >禁用
              </el-button> -->
                <!-- <el-button
                v-if="scope.row.isEffect === '0'"
                :disabled="scope.row.disabled"
                type="text"
                size="small"
                @click="edit(scope.row)"
              >启用
              </el-button> -->
                <el-button v-if="scope.row.isEffect === '1'" type="text" size="small" @click="showGroupTemplate(scope.row)">源码
                </el-button>
                <el-button type="text" size="small" @click="showResult(scope.row)">查看
                </el-button>
              </template>
            </el-table-column>
          </el-table>
          <!-- 分页 -->
          <el-pagination class="float-right mt-2" @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="page.pageIndex" :page-sizes="[10, 20, 50, 100]" :page-size="page.pageSize" :total="page.totalPage" layout="total, sizes, prev, pager, next, jumper">
          </el-pagination>
        </div>
      </div>
    </div>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    <!-- 弹窗, 测试-->
    <test-condition v-if="testConditionVisible" ref="testCondition"></test-condition>

    <el-dialog title="模板预览" :visible.sync="showVisbale" width="60%">
      <pre>{{ templateStr }}</pre>
      <span slot="footer" class="dialog-footer">
        <el-button @click="showVisbale = false">取 消</el-button>
        <el-button :plain="true" type="info" @click="showVisbale = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  pageData,
  updateData,
  showTemplate,
  listGroupData,
  gettaskList,
  copyPublishUpdate,
} from "@/api/ruleConfig/groupInfo";
import {
  downAllTemplate
} from "@/api/ruleConfig/decision";
import AddOrUpdate from "./add-or-update";
import TestCondition from "./test-condition";
import { mapGetters } from "vuex";
import constant from "../../../../../api/constant";

export default {
  data() {
    return {
      taskinfo2: {},
      checktaskarry: [],
      btnBoolean: true,
      allGroupInfo: [],
      loading: false,
      templateStr: "",
      showVisbale: false,
      dataForm: {
        groupName: "",
        groupIdentify: "",
      },
      dataList: [],
      dataListLoading: false,
      addOrUpdateVisible: false,
      testConditionVisible: false,
      page: {
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
      },
    };
  },
  //利用计算属性
  computed: {
    taskinfo() {
      return this.$store.state.checktaskInfo;
    },
  },
  //监听执行
  watch: {
    taskinfo: {
      handler: function (newVal, oldVal) {
        // console.log(newVal, oldVal);
        this.taskinfo2 = newVal;
        this.getDataList();
        // console.log(newVal.status, "2222");
        if (newVal.status == "1") {
          this.btnBoolean = false;
        } else {
          this.btnBoolean = true;
          this.$message.warning("该任务已发布，不能新增或编辑!");
        }
      },
      deep: true,
    },
    taskinfo2: {
      handler: function (newVal, oldVal) { },
      deep: true,
    },
  },
  created() {
    this.taskinfo2 = this.$store.state.checktaskInfo;
    // console.log(this.$store.state.checktaskInfo, "2222");
    if (this.taskinfo2 && this.taskinfo2.taskNo) {
      this.btnBoolean = false;
    }
    this.getDataList();
    //   gettaskList({}).then((res) => {
    //   if (res.data.data) {
    //     this.checktaskarry = res.data.data;
    //   }
    // });
  },
  mounted() { },

  components: {
    AddOrUpdate,
    TestCondition,
  },
  activated() { },

  methods: {
    initGroupInfo() {
      listGroupData({}).then(() => {
        this.allGroupInfo = res.data.data;
      });
    },
    // 启用禁用
    edit(row) {
      let data = row;
      if (row.isEffect === "0") {
        data.isEffect = "1";
      } else {
        data.isEffect = "0";
      }
      let param = JSON.parse(JSON.stringify(data));
      if (param.type === "p") {
        param.taskNo = this.taskinfo2.taskNo;
        copyPublishUpdate(JSON.stringify(param)).then((res) => {
          this.getDataList();
        });
      } else {
        updateData(JSON.stringify(param)).then((res) => {
          this.getDataList();
        });
      }
    },
    // 重置查询条件
    resetData() {
      this.dataForm.groupName = "";
      this.dataForm.groupIdentify = "";
    },

    showResult(data) {
      // console.log(data);
      if (this.taskinfo2 && this.taskinfo2.taskNo) {
      } else {
        data.id = data.ID;
      }
      this.$router.push({
        path: "/drools-ruleConfig/group/view/" + data.id + "/" + data.type,
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.page.pageSize = val;
      this.page.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.page.pageIndex = val;
      this.getDataList();
    },
    // 获取数据列表
    getDataList() {
      // console.log(this.taskinfo2);
      this.$store.dispatch("loading/CHANGE_LOADING", true);
      let params = {
        pageNumber: this.page.pageIndex,
        pageSize: this.page.pageSize,
        groupName: this.dataForm.groupName,
        groupIdentify: this.dataForm.groupIdentify,
        taskNo: this.taskinfo2.taskNo,
        channelCode: this.$store.state.channel.channelCode,
      };
      pageData(params)
        .then((res) => {
          this.$store.dispatch("loading/CHANGE_LOADING", false);
          this.dataList = res.data.data.list;
          this.page.totalPage = res.data.data.totalCount;
          if (this.taskinfo2.status == "1") {
            this.dataList.forEach((e) => {
              e.disabled = false;
            });
          }
          if (this.taskinfo2.status !== "1") {
            this.dataList.forEach((e) => {
              e.disabled = true;
            });
          }
        })
        .catch((err) => {
          this.$store.dispatch("loading/CHANGE_LOADING", false);
        });
    },
    // 预览drools模板
    showGroupTemplate(data) {
      // console.log(data.id,'data');
      // if (this.taskinfo2 && this.taskinfo2.taskNo) {
      // } else {
      //   data.id = data.ID;
      // }
      this.$store.dispatch("loading/CHANGE_LOADING", true);
      let params = {
        type: data.type,
        id: data.id + "",
      };
      showTemplate(params)
        .then((res) => {
          this.$store.dispatch("loading/CHANGE_LOADING", false);
          this.templateStr = res.data.data;
          this.showVisbale = true;
        })
        .catch((err) => {
          this.$store.dispatch("loading/CHANGE_LOADING", false);
        });
    },

    // 新增 / 修改
    addOrUpdateHandle(data) {
      // console.log(data);
      constant.clearTag(this);
      if (data) {
        this.$router.push({
          path:
            "/drools-ruleConfig/group/add-or-update/" +
            data.id +
            "/" +
            data.type,
        });
      } else {
        this.$router.push({
          path: "/drools-ruleConfig/group/add-or-update",
        });
      }
    },
    // 删除
    deleteHandle(id) {
      this.$confirm(`确定对[id=${id}]进行[删除]操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let params = {
            id: id + "",
          };
          del(params).then((res) => {
            if (res.data && res.data.resultCode === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.getDataList();
                },
              });
            } else {
              this.$message.error(res.data.resultMsg);
            }
          });
        })
        .catch(() => { });
    },
    editFlow(id) {
      this.$router.push({ name: "demo-X6", query: { id: id } });
    },
    downExcel() {
      let taskInfo = this.$store.state.checktaskInfo;
      let params = {
        taskNo: taskInfo.taskNo,
      };
      downAllTemplate(params).then((res) => { });
    },
  },
  filters: {
    isEffectFilter: function (value) {
      let label = "";
      if (value === "0") {
        label = "失效";
      } else if (value === "1") {
        label = "有效";
      }
      return label;
    },
    groupTypeFilter: function (value) {
      let label = "";
      if (value === "1") {
        label = "普通形式";
      } else if (value === "2") {
        label = "决策表";
      }
      return label;
    },
  },
};
</script>
<style lang="less" scoped>
body .el-table th.gutter {
  display: table-cell !important;
}

.current-task {
  margin: 20px;

  span {
    font-size: 16px;
    font-weight: 900;
    margin-right: 10px;
  }

  .el-dropdown span {
    font-size: 14px;
    color: #ff0000;
  }
}
</style>
